﻿using System;
using System.Threading.Tasks;

using Linq2Crm.ObjectModel;
using Linq2Crm.ObjectModel.DomainObject;

namespace Linq2Crm.Request
{
	public class SetStatusRequest : RequestBase
	{
		protected SetStatusRequest(Type type) : base(type)
		{
		}

		internal SetStatusRequest(Type type, RecordStateCode stateCodeCode, int statusCode)
			: this(type)
		{
			StateCode = (int) stateCodeCode;
			StatusCode = statusCode;
		}

		public IDomainObject Entity { get; set; }

		public Action<SetStatusResponse> Callback { get; set; }

		public int StatusCode { get; set; }

		public int StateCode { get; set; }

		public static SetStatusRequest Create<T>(IDomainObject entity, RecordStateCode stateCode, int statusCode)
			where T : IDomainObject
		{
         if (entity == null)
         {
            throw new ArgumentNullException();
         }

			return new SetStatusRequest(typeof (T))
			       	{
			       		StateCode = (int) stateCode,
			       		StatusCode = statusCode,
			       		Entity = entity
			       	};
		}

		internal override void Execute()
		{
			DomainObjectContext.Instance.ExecuteSetStatusRequest(this);
		}

		internal override async Task<ResponseBase> ExecuteAsync()
		{
			var reponse = await DomainObjectContext.Instance.ExecuteSetStatusRequestAsync(this);
			return reponse;
		}
	}
}